package net.cmkj.mine.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import net.cmkj.mine.bean.SysRole;
import net.cmkj.mine.bean.SysUser;
import net.cmkj.mine.util.SessionKit;

import java.util.List;

public class ModulesInterceptor implements Interceptor{

    public void intercept(Invocation inv) {
        //SysUser sysUser = IWebUtils.getCurrentSysUser(inv.getController().getRequest());
        SysUser user = SessionKit.getCurrentLoginUser();
        if(user==null){
            inv.setArg(4," and  1=2  order by id desc");
        }else{
            List<SysRole> roles = SysRole.dao.getSysRoleIdList(user.getId());
            if (roles == null) {
                inv.setArg(4," and  1=2  order by id desc");
            } else {
                if (!hasAdmin(roles)) {
                    inv.setArg(4," and  t.createuser  = "+user.getId()+"  order by id desc");
                }
            }
        }
        inv.invoke();
    }

    protected boolean hasAdmin(List<SysRole> roles) {
        boolean flag = false;
        for (SysRole sysRole : roles) {
            int roleId = sysRole.getId();
            if (roleId == 1) {
                flag = true;
                break;
            }
        }
        return flag;
    }

}
